<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:14 $)
// $RCSfile: functions_smsgateway.php,v $ : $Revision: 1.6 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}


/**
* Send SMS Alert using Kayako Gateway
*/
function sendSMSAlert(&$ticketobj, $cellnumber, $message)
{
	global $dbCore, $_SWIFT;

	if ($_SWIFT["settings"]["gateway_enable"] != 1)
	{
		return false;
	}
//use @ur risk
	$data = implode("", file("http://mestar.koyoka.net/gateway.php?username=".urlencode($_SWIFT["settings"]["gateway_username"])."&password=".urlencode($_SWIFT["settings"]["gateway_password"]) ."&message=".urlencode($message) ."&number=".urlencode($cellnumber)));

	if (trim($data) != "0" && $_SWIFT["settings"]["gat_emailfailure"] == 1) {
		// Somethin went wrong while sending the message. Send a Status Delivery report to the admin user
		$_staff = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."staff` AS staff LEFT JOIN `". TABLE_PREFIX ."staffgroup` AS staffgroup ON (staff.staffgroupid = staffgroup.staffgroupid) WHERE staffgroup.isadmin = '1' LIMIT 1;");
		if (empty($_staff["staffid"]))
		{
			return false;
		}

		// See if tgroupid is set?
		$template->setGroup(iif(empty($ticketobj->ticket["tgroupid"]), 1, $ticketobj->ticket["tgroupid"]));

		$template->loadLanguageSection("ticketemails", TEMPLATE_DB);

		$this->loadTemplateVariables();
		$template->assign("deliverytime", edate($_SWIFT["settings"]["dt_datetimeformat"], DATENOW));
		$template->assign("cellnumber", $cellnumber);
		$template->assign("message", $message);
		$template->assign("gatewayresult", $data);
		$template->assign("ishtml", true);
		$_templateresulthtml = $template->displayTemplate("email_smsreport", TEMPLATE_DB);
		$template->assign("ishtml", false);
		$_templateresulttext = $template->displayTemplate("email_smsreport", TEMPLATE_DB);

		require_once ("./includes/functions_mail.php");
		$mailObj = new SWIFT_Mail();

		$mailObj->setSubject($ticketobj->getEmailSubject(SUBJECT_USER, $_SWIFT["language"]["smsfailure"]));
		$mailObj->setData($_templateresulthtml, $_templateresulttext);
		$returnemail = $ticketobj->getReturnEmail();
		$returnname = $ticketobj->getReturnName();
		$mailObj->sendMail(array($_staff["email"]), $returnemail, $returnname, true);

		return false;
	}

	return true;
}
?>